<?php
//短信验证码模型

class ValidateRecordModel extends MY_Model {

	
	function __construct(){
		parent::__construct();
		
		$this->table = 'validate_record';

	}
	
	/**
	 * 保存一个验证记录到数据库，如果存在则更新
	 *
	 * @param string $key
	 *        	验证标识
	 * @param string $code
	 *        	验证值
	 * @param string $type
	 *        	验证类型
	 * @param datetime $expired_time
	 *        	过期时间
	 * @param array $ext_info
	 *        	扩展信息
	 */
	function save_validate_record ($key, $code, $type='', $last_send_time=0, $expired_time='', $ext_info = array())
	{
		$record = array(
			// 验证代码
			"record_code" => $code,
			// 业务类型
			"record_type" => $type,
			// 业务类型
			"last_send_time" => $last_send_time,
			// 过期时间
			"expired_time" => $expired_time,
			// 扩展信息
			"ext_info" => serialize($ext_info)
		);
		
		$record["create_time"] = time();
		$exist = $this->check_validate_record_exist($key,$type);
		
		if(! $exist)
		{
			$record['record_key'] = $key;
			// 记录创建时间
			
			
			/* insert */
			$this->db->insert($this->table, $record);
		}
		else
		{
			/* update */
			$this->db->update($this->table, $record,"record_key = '$key'");
		}
		
		//发送验证码
		$this->send_message($key,$code);
		
		return true;
	}

	/**
	 * 检查验证记录在数据库中是否已经存在
	 *
	 * @param string $key        	
	 * @return boolean
	 */
	function check_validate_record_exist ($key,$type='')
	{
		//$sql = "select count(*) from " . $this->ecs->table($this->table) . " where record_key = '{$key}'";
		//$count = $this->db->getOne($sql);
		
		$count = $this->getCount(array('record_key'=>$key));
		
		if($count > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 从数据库中删除过期的验证记录
	 */
	function delete_validate_record ()
	{
		$current_time = time();
		//$sql = "delete from " . $this->ecs->table($this->table) . " where expired_time < '$current_time'";
		
		$this->db->where("expired_time < '$current_time'");
		
		return $this->db->delete($this->table);
	}

	/**
	 * 根据键删除验证记录
	 * 
	 * @param string $key        	
	 */
	function remove_validate_record ($key,$type)
	{
		//$sql = "delete from " . $this->ecs->table($this->table) . " where record_key = '$key'";
		
		$this->db->where(array('record_key'=>$key,'record_type'=>$type));
		return $this->db->delete($this->table);
	}

	/**
	 * 基本验证
	 * 
	 * @param string $key        	
	 * @param string $value        	
	 * @return int 0-验证信息不存在，1-验证码已过期, 2-验证码错误
	 */
	function validate_code ($key, $code,$type='')
	{
		$record = $this->get_validate_record($key,$type);
		
		$time = time();
		
		
		
		if($record == false)
		{
			return 0;
		}
		//else if( $time -$record['create_time'] >600){//大于5分钟的
			//$this->remove_validate_record($key,$type);
		//	return 1;
		//}
		//不需要时间验证
		else if(!empty($record['expired_time']) && $record['expired_time'] < time())
		{
			return 1;
		}
		
		else if($record['record_code'] != $code)
		{
			return 2;
		}
		else
		{
			$this->remove_validate_record($key,$type);
			return true;
		}
	}

	/**
	 * 从数据库中获取验证记录信息，会将ext_info数组解析与结果合并
	 * 
	 * @param string $key        	
	 * @return boolean|array:
	 */
	function get_validate_record ($key,$type='')
	{
		//$sql = "select * from " . $this->ecs->table($this->table) . " where record_key = '$key'";
		//$row = $this->db->getRow($sql);
		
		$row = $this->getRow(array('record_key'=>$key,'record_type'=>$type));
		
		
		if($row == false){
			return false;
		}

		
		$row['ext_info'] = unserialize($row['ext_info']);
		
		$record = array(
			// 验证代码
			"record_key" => $row['record_key'],
			// 验证代码
			"record_code" => $row['record_code'],
			// 业务类型
			"record_type" => $row['record_type'],
			// 开始时间
			"last_send_time" => $row['last_send_time'],
			// 过期时间
			"expired_time" => $row['expired_time'],
			// 创建时间
			"create_time" => $row['create_time']
		);
		
		//$record = array_merge($record, $row['ext_info']);
		
		return $record;
	}
	//检测验证码
	function mobile_record($key,$code){
		//$sql = "select * from " . $this->ecs->table($this->table) . " where record_key = '$key' and record_code='$code'";
		$row = $this->getRow(array('record_key'=>$key,'record_code'=>$code));		
		return $row;
	}
	//发送验证码至手机
	function send_message($mobile,$code){
		if(empty($mobile) or empty($code)){
			return '';
		}
		
		//1.请求url地址
		$base_url = base_url('');
		$url = $base_url."plugins/sms/index.php?mobile=".$mobile."&code=".$code;
		
		
		//2.初始化curl
		$ch = curl_init($url);
		//3.设置参数
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //跳过证书验证
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
		//4.调用接口
		$res = curl_exec($ch);	
		if(curl_errno($ch)){
			var_dump(curl_error($ch));
		}	
		$resArr = json_decode($res,true);
		curl_close($ch);
		
		return $resArr['status']=='success'?$resArr:'';
	}
	
}
